package com.hjrpc.babyyamall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.hjrpc.babyyamall.product.dao.CategoryDao;
import com.hjrpc.babyyamall.product.entity.AttrEntity;
import com.hjrpc.babyyamall.product.service.CategoryService;
import com.hjrpc.babyyamall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hjrpc.babyyamall.product.entity.AttrGroupEntity;
import com.hjrpc.babyyamall.product.service.AttrGroupService;
import com.hjrpc.common.utils.PageUtils;
import com.hjrpc.common.utils.R;

import javax.validation.constraints.NotNull;


/**
 * 属性分组
 *
 * @author hujian
 * @email 502361472@qq.com
 * @date 2021-08-07 12:54:21
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Integer catelogId) {

        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }

    @RequestMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId) {

        List<AttrEntity> resultList = attrGroupService.listAttr(attrGroupId);

        return R.ok().put("data",resultList);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        //查出完整的catelogPath,方便前端回显
        attrGroup.setCatelogPath(categoryService.findCatelogPath(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 信息
     */
    @RequestMapping("/{catelogId}/withattr")
    public R listWithAttr(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupWithAttrsVo> attrGroup = attrGroupService.listWithAttr(catelogId);
        return R.ok().put("data", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }
//
//    @RequestMapping("/update")
//    public R update(@RequestBody AttrGroupEntity attrGroup) {
//        attrGroupService.updateById(attrGroup);
//
//        return R.ok();
//    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
